local Archy = LibStub("AceAddon-3.0"):GetAddon("Archy")
local Navigation = Archy:NewModule("Navigation", "AceEvent-3.0", "AceTimer-3.0")
local Digsite = Archy:GetModule("Digsite")
local L = LibStub("AceLocale-3.0"):GetLocale("Archy")

local db

function Navigation:OnInitialize()
	self.frame = nil
	self.point = nil
	self.visible = true
end

function Navigation:OnEnable()
	db = Archy.db.profile
	self.TomTomExists = false
	if TomTom and TomTom.AddZWaypoint and TomTom.RemoveWaypoint then 
		self.TomTomExists = true 
	end

	Archy.archData.RegisterCallback(self, "PlayerCastSurvey", "OnPlayerSurveyed")
	Archy.archData.RegisterCallback(self, "DigSitesUpdated", "OnDigSitesUpdated")
	self:RegisterMessage("ArchyNearestDigSiteChanged", "OnSiteChange")
	self:RegisterMessage("ArchyConfigChanged", "OnConfigChanged")

	self:EnableDisable()
end

function Navigation:OnDisable()
	Archy.archData.UnregisterCallback(self, "PlayerCastSurvey")
	Archy.archData.UnregisterCallback(self, "DigSitesUpdated")
	self:UnregisterMessage("ArchyNearestDigSiteChanged")
	self:UnregisterMessage("ArchyConfigChanged")
end

function Navigation:OnPlayerSurveyed()
	
	if db.tomtom.enable and self.TomTomExists then 
		self.visible = false
		self:DisableTomTom()
	end
end

function Navigation:OnDigSitesUpdated()
	if db.tomtom.enable and self.TomTomExists and not self.visible then 
		self.visible = true 
		self:EnableTomTom()
	end
end

function Navigation:OnConfigChanged()
	db = Archy.db.profile
	self:EnableDisable()
	self:SetupArrivalDistance()
end

function Navigation:SetupArrivalDistance()
	if db.tomtom.enable and self.TomTomExists then
		if TomTom.profile then
			TomTom.profile.arrow.arrival = db.tomtom.arrivalDistance
			TomTom.profile.arrow.enablePing = db.tomtom.arrivalPing
		end
	end
end

function Navigation:DisableTomTom()
	if self.frame then
		if self.point then
			self.point = TomTom:RemoveWaypoint(self.point)
			self.point = nil
		end
		self.frame:Hide()
		self.frame:SetScript("OnUpdate", nil)
		self.frame = nil
	end
end

function Navigation:EnableTomTom()
	self:OnSiteChange()
end

function Navigation:EnableDisable()
	if not db.general.show or not db.tomtom.enable or not self.TomTomExists or not self.visible  then
		self:DisableTomTom()
	else
		self:EnableTomTom()
	end
end

local lastSite
function Navigation:OnSiteChange(event, site)
	if not lastSite and not site then return end
	if not site then site = lastSite else lastSite = site end
	if not db.tomtom.enable then return end
	if not db.general.show then return end
	
	if self.TomTomExists then
		if not self.frame then
			local t = 0
			self.frame = CreateFrame("Frame")
			self.frame:Show()
		end
		local waypointExists = false
		if TomTom.WaypointExists then
			waypointExists = TomTom:WaypointExists(site.continent, site.zone.id, site.location.x * 100, site.location.y * 100, site.name)
		end
		if not waypointExists then
			if self.point then
				self.point = TomTom:RemoveWaypoint(self.point)
			end
			self.point = TomTom:AddZWaypoint(site.continent, site.zone.id, site.location.x * 100, site.location.y * 100, site.name, false, false, false, false, false, true)
		end
	else
		Archy.db.profile.tomtom.enable = false
		self:OnConfigChanged()
	end
end